from django.http import HttpResponse
from django.shortcuts import render, redirect

# Create your views here.
from account.service import getPwd
from spiders.services.heathdaka import daka
from spiders.services.translate import translate
from spiders.services.washer.washer import getDevices, order
from spiders.services.weibo import weiboHotSearch


def washer_main(request):
    me = request.COOKIES.get('me')
    if me:
        try:
            if getPwd(me.split('^')[0]) == me.split('^')[1]:
                devices = getDevices()
                return render(request, 'washer/index.html', {"devices": devices})
            else:
                return redirect('/signin')
        except:
            return redirect('/signin')
    else:
        return redirect('/signin')


def washer_order(request):
    me = request.COOKIES.get('me')
    if me:
        try:
            if getPwd(me.split('^')[0]) == me.split('^')[1]:
                deviceId = request.GET.get("deviceId")
                storeId = request.GET.get("storeId")
                orderResult = order(deviceId, storeId)
                return render(request, 'washer/result.html', {"orderResult": orderResult})
            else:
                return redirect('/signin')
        except:
            return redirect('/signin')
    else:
        return redirect('/signin')


def translater_main(request):
    me = request.COOKIES.get('me')
    if me:
        try:
            if getPwd(me.split('^')[0]) == me.split('^')[1]:
                return render(request, 'translate/translation.html')
            else:
                return redirect('/signin')
        except:
            return redirect('/signin')
    else:
        return redirect('/signin')


def translater_run(request):
    me = request.COOKIES.get('me')
    if me:
        try:
            if getPwd(me.split('^')[0]) == me.split('^')[1]:
                translation = request.POST.get("translation")
                result = translate.run(translation)
                res = HttpResponse({result})
                return res
            else:
                return redirect('/signin')
        except:
            return redirect('/signin')
    else:
        return redirect('/signin')


def healthdaka_main(request):
    me = request.COOKIES.get('me')
    if me:
        try:
            if getPwd(me.split('^')[0]) == me.split('^')[1]:
                return render(request, 'healthdaka/healthdaka.html')
            else:
                return redirect('/signin')
        except:
            return redirect('/signin')
    else:
        return redirect('/signin')


def healthdaka_run(request):
    me = request.COOKIES.get('me')
    if me:
        try:
            if getPwd(me.split('^')[0]) == me.split('^')[1]:
                xh = request.POST.get('xh')
                mm = request.POST.get('mm')
                dqszdmc = request.POST.get('dqszdmc')
                dqszsfdm = request.POST.get('dqszsfdm')
                dqszsdm = request.POST.get('dqszsdm')
                dqszxdm = request.POST.get('dqszxdm')
                result = daka.addUser(xh, mm, dqszdmc, dqszsfdm, dqszsdm, dqszxdm)
                res = HttpResponse({result})
                return res
            else:
                return redirect('/signin')
        except:
            return redirect('/signin')
    else:
        return redirect('/signin')


def nyancat_main(request):
    me = request.COOKIES.get('me')
    if me:
        try:
            if getPwd(me.split('^')[0]) == me.split('^')[1]:
                return render(request, 'nyancat/index.html')
            else:
                return redirect('/signin')
        except:
            return redirect('/signin')
    else:
        return redirect('/signin')


def mikutap_main(request):
    me = request.COOKIES.get('me')
    if me:
        try:
            if getPwd(me.split('^')[0]) == me.split('^')[1]:
                return render(request, 'mikutap/index.html')
            else:
                return redirect('/signin')
        except:
            return redirect('/signin')
    else:
        return redirect('/signin')


def weibo_main(request):
    me = request.COOKIES.get('me')
    if me:
        try:
            if getPwd(me.split('^')[0]) == me.split('^')[1]:
                result = weiboHotSearch.run()
                return render(request, 'weibo/index.html',{'result':result})
            else:
                return redirect('/signin')
        except:
            return redirect('/signin')
    else:
        return redirect('/signin')
